package dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Class;
import model.Parents;


public class ParentsMod {
	 Connection connection;
	 private static ParentsMod isntance;
		public static ParentsMod getIsntance() {
			if (isntance == null) {
				isntance = new ParentsMod();
			}
			return isntance;
		}
	 public boolean isPID(int pid) throws IOException{ /*kiem tra tinh hop le cua pid va co nam trong DB hay khong*/
			connection =  DB_Connection.getIsntance().getConnection();
			PreparedStatement sqlp = null;
			boolean result = false;
			try {
				sqlp = connection.prepareStatement("SELECT pid FROM parents where pid = ? ");
				sqlp.setInt(1, pid);
				ResultSet rs = sqlp.executeQuery();
				while (rs.next()) {
					result = true;
				}
				sqlp.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}
	 public boolean isExitUidInTable(int uid){
			PreparedStatement sql = null;
			boolean result = false;
			connection =  DB_Connection.getIsntance().getConnection();
			try {
				sql = connection.prepareStatement("SELECT uid FROM parents where uid = ? ");
				sql.setInt(1, uid);
				ResultSet rs = sql.executeQuery();
				while (rs.next()) {
					result = true;
				}
				sql.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
				
			}
			return result;
		}
	 public boolean addParent(Parents parents) {
			PreparedStatement sqlr = null;
			boolean result = false;
			try {
				connection = DB_Connection.getIsntance().getConnection();
				String sql = "INSERT INTO parents(uid,  name,  birthday,  sex, avartar,  bio,  email, address ) VALUES(? , ? , ? , ? , ? , ? , ? ,?) ";
				sqlr = connection.prepareStatement(sql);
				sqlr.setInt(1, parents.getUid());
				sqlr.setString(2, parents.getName());
				sqlr.setString(3, parents.getBirthday());
				sqlr.setInt(4, parents.getSex());
				sqlr.setString(5, parents.getAvartar());
				sqlr.setString(6, parents.getBio());
				sqlr.setString(7, parents.getEmail());
				sqlr.setString(8, parents.getAddress());
				int number = sqlr.executeUpdate();
				if (number > 0) {
					result = true;
				}
				sqlr.close();
				connection.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
			return result;
		}
	 public boolean UpdateParentByPid(Parents parents, int pid) {
			PreparedStatement sqlr = null;
			boolean result = false;
			try {
				connection = DB_Connection.getIsntance().getConnection();
				String sql = "UPDATE parents SET uid = ? ,  name =? ,  birthday = ? ,  sex = ? , avartar = ?,  bio = ?,  email = ?, address =? WHERE pid = ? LIMIT = 1 ";
				sqlr = connection.prepareStatement(sql);
				sqlr.setInt(1, parents.getUid());
				sqlr.setString(2, parents.getName());
				sqlr.setString(3, parents.getBirthday());
				sqlr.setInt(4, parents.getSex());
				sqlr.setString(5, parents.getAvartar());
				sqlr.setString(6, parents.getBio());
				sqlr.setString(7, parents.getEmail());
				sqlr.setString(8, parents.getAddress());
				sqlr.setInt(9, pid);
				int number = sqlr.executeUpdate();
				if (number > 0) {
					result = true;
				}
				sqlr.close();
				connection.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
			return result;
		}
	 public boolean UpdateParentByUid(Parents parents, int u) {
			PreparedStatement sqlr = null;
			boolean result = false;
			try {
				connection = DB_Connection.getIsntance().getConnection();
				String sql = "UPDATE parents SET uid = ? ,  name =? ,  birthday = ? ,  sex = ? , avartar = ?,  bio = ?,  email = ?, address =? WHERE uid = ? LIMIT = 1 ";
				sqlr = connection.prepareStatement(sql);
				sqlr.setInt(1, parents.getUid());
				sqlr.setString(2, parents.getName());
				sqlr.setString(3, parents.getBirthday());
				sqlr.setInt(4, parents.getSex());
				sqlr.setString(5, parents.getAvartar());
				sqlr.setString(6, parents.getBio());
				sqlr.setString(7, parents.getEmail());
				sqlr.setString(8, parents.getAddress());
				sqlr.setInt(9, u);
				int number = sqlr.executeUpdate();
				if (number > 0) {
					result = true;
				}
				sqlr.close();
				connection.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
			return result;
		}
	 public Parents getParentByUid(int u){
		PreparedStatement sqls =null;
		Parents result = null;
		try {
			connection = DB_Connection.getIsntance().getConnection();
			sqls = connection.prepareStatement("SELECT pid, uid, name, birthday, sex, avartar, bio, email, address FROM parents WHERE uid = ?");
			sqls.setInt(1, u);
			ResultSet rs = sqls.executeQuery();
			while (rs.next()) {
				int pid = rs.getInt(1);
				int uid = rs.getInt(2);
				String name = rs.getString(3);
				String birthday = rs.getString(4);
				int sex = rs.getInt(5);
				String avartar = rs.getString(6);
				String bio = rs.getString(7);
				String email = rs.getString(8);	
				String address = rs.getString(9);
				result = new Parents(pid, uid, name, birthday, sex, avartar, bio, email, address);
			}
			sqls.close();
			connection.close();
		} catch (Exception e) {
		}
		return result;
	 }
	 public Parents getParentByPid(int p){
			PreparedStatement sqls =null;
			Parents result = null;
			try {
				connection = DB_Connection.getIsntance().getConnection();
				sqls = connection.prepareStatement("SELECT pid, uid, name, birthday, sex, avartar, bio, email, address FROM parents WHERE pid = ?");
				sqls.setInt(1, p);
				ResultSet rs = sqls.executeQuery();
				while (rs.next()) {
					int pid = rs.getInt(1);
					int uid = rs.getInt(2);
					String name = rs.getString(3);
					String birthday = rs.getString(4);
					int sex = rs.getInt(5);
					String avartar = rs.getString(6);
					String bio = rs.getString(7);
					String email = rs.getString(8);	
					String address = rs.getString(9);
					result = new Parents(pid, uid, name, birthday, sex, avartar, bio, email, address);
				}
				sqls.close();
				connection.close();
			} catch (Exception e) {
			}
			return result;
		 }
	 public boolean delByPid(int pid){
				PreparedStatement delpd = null;
				boolean result = false;
				Connection connection = DB_Connection.getIsntance().getConnection();
				try {
					
					delpd = connection.prepareStatement("DELETE FROM parents WHERE pid = ? LIMIT 1");
					delpd.setInt(1, pid);
					int number = delpd.executeUpdate();
					if (number > 0) {
						result = true;
					}
					delpd.close();
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return  result;
			}
	 public boolean delByUid(int uid){
			PreparedStatement delpd = null;
			boolean result = false;
			Connection connection = DB_Connection.getIsntance().getConnection();
			try {
				
				delpd = connection.prepareStatement("DELETE FROM parents WHERE uid = ? LIMIT 1");
				delpd.setInt(1, uid);
				int number = delpd.executeUpdate();
				if (number > 0) {
					result = true;
				}
				delpd.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return  result;
		}
}
